//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj首页

#import "HomeController.h"
#import "SettingController.h"
#import "MyProfileController.h"
#import "AddMemberVC.h"
#import "MembersManageVC.h"
#import "MJCategoryRecordVC.h"
#import "MemberSelectAlertView.h"
#import "DetectionPeriodtAlertView.h"
#import "NotesGuideAlertView.h"
#import "NotesGuideVC.h"
#import "ReplaceResultsAlertView.h"
#import "MemberSelectDateAlertView.h"
#import "MJPickerViewAlertView.h"
#import "SetLoginPasswordVC.h"

@interface HomeController ()
/// 滚动视图，用于展示可滚动的内容
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@end

@implementation HomeController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //更新版本
    [HealthHelper appUpgradeWithToast:false];
    
    [WCBDHelper.shared mj_initPatientArray];
    
    MJUIAfter_GCD(0.1, ^{
        [self initNotesAlertView];
    });
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [WCBDHelper.shared initHealthData];
    
    [self requestUserData];
}

- (void)mj_setUI{
    [super mj_setUI];
    
#ifdef mjdebug
    MJUIAfter_GCD(0.1, ^{
                        [self pushViewController:@"IBMMSimpleVC"];
//                        [self pushViewController:@"NotesGuideVC"];
        
        
//        MJMultipleModel *mModel = [self createMJMultipleModel];
//        [WCBDHelper.shared mj_insertMultipleObjectWithModel:mModel];
//        [self xxx111];
    });
#endif
    
    [self.navigationController setNavigationBarHidden:true animated:NO];
    [self setContentInsetNever:self.scrollView];
    self.scrollView.showsVerticalScrollIndicator = false;
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
    NSLog(@"MJToken=======================%@",UserManager.shared.userToken);
//    MJWeakSelf
//    [RACObserve(LanguageHelper.shared, language) subscribeNext:^(NSString *language) {
//        weakSelf.iconImageView.image = [HealthHelper mj_imageWithLocalizedNamed:@"bg_banner"];
//    }];
}

#pragma mark - 方法
- (void)initNotesAlertView{
  
    MJWeakSelf
    if(![[MJUIUserDeFault objectForKey:SetPassword_Open] isEqualToString:@"1"] && !UserManager.shared.userModel.havePassword) {
        
        MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
        configure.confirmButtonColor = UIColor.mj_appColor;
        configure.confirmTitle = MJUILocalizedString(@"Set Password");
        configure.cancelTitle = MJUILocalizedString(@"Not Now");
        configure.messageAlignmentCenter = true;
        configure.contentView = self.view;
        NSString *messageString = MJUILocalizedString(@"To ensure your account security, set a password now.");
        [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Login Password Not Set") message:messageString configure:configure confirmBlock:^{
            [MJUIUserDeFault setObject:@"1" forKey:SetPassword_Open];

            SetLoginPasswordVC *vc = [[SetLoginPasswordVC alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:true];

        } cancelBlock:^{
            [MJUIUserDeFault setObject:@"1" forKey:SetPassword_Open];

        }];
    }
    if(![[MJUIUserDeFault objectForKey:NotesGuide_Open] isEqualToString:@"1"]) {
        UITabBarController *tabBarController = [self findTabBarController];

        NotesGuideAlertView *notesGuideAlertView = [[NotesGuideAlertView alloc] init];
        notesGuideAlertView.alertViewType = CenterAlertViewTypeGuide;
        notesGuideAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        notesGuideAlertView.confirmBlock = ^{
            NotesGuideVC *vc = [[NotesGuideVC alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:true];
        };
        tabBarController.modalPresentationStyle = UIModalPresentationCurrentContext;
        [tabBarController presentViewController:notesGuideAlertView animated:NO completion:nil];
    }
}

- (void)requestUserData{
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:user_update requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        if(model.code == 200){
            
        }else if (model.code ==  401){
        }
    } failure:^(NSError *error) {
        
    }];
}

- (IBAction)handleRecordsButtonEvent:(UIButton *)sender {
    MJCategoryRecordVC *vc = [[MJCategoryRecordVC alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleMembersManagerButtonEvent:(UIButton *)sender {
    MembersManageVC *vc = [[MembersManageVC alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleAddMemberButtonEvent:(UIButton *)sender {
    AddMemberVC *vc = [[AddMemberVC alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleMultipleTestsButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeMultiple];
}

- (IBAction)handleUATestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeGout];
}

- (IBAction)handleBGTestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeGlu];
}

- (IBAction)handleHBTestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeHB];
}

- (IBAction)handleBLTestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeTC];
}

- (void)handleItemTestButtonClick:(DetectionItemType)detectionItemType {
    
    UITabBarController *tabBarController = [self findTabBarController];
    
    MJWeakSelf
    MemberSelectAlertView *memberSelectAlertView = [[MemberSelectAlertView alloc] init];
    memberSelectAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    tabBarController.modalPresentationStyle = UIModalPresentationCurrentContext;
    [tabBarController presentViewController:memberSelectAlertView animated:NO completion:nil];
    memberSelectAlertView.handleAddMemberBlock = ^{
        AddMemberVC *vc = [[AddMemberVC alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    memberSelectAlertView.confirmBlock = ^(PatientModel *patientModel) {
        DetectionPeriodtAlertView *detectionPeriodtAlertView = [[DetectionPeriodtAlertView alloc] init];
        detectionPeriodtAlertView.confirmBlock = ^(NSString *period) {
            [HealthHelper jumpDetectionViewController:patientModel detectionItemType:detectionItemType period:period viewController:weakSelf];
        };
        detectionPeriodtAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        tabBarController.modalPresentationStyle = UIModalPresentationCurrentContext;
        [tabBarController presentViewController:detectionPeriodtAlertView animated:NO completion:nil];
    };
}

- (UITabBarController *)findTabBarController {
    UIViewController *parent = self.parentViewController;
    while (parent != nil) {
        if ([parent isKindOfClass:[UITabBarController class]]) {
            return (UITabBarController *)parent;
        }
        parent = parent.parentViewController;
    }
    return nil;
}
#pragma mark - 代理

#pragma mark - 懒加载

@end
